#include "usart.h"
#include "gpio.h"
#include "stdio.h"

UART_HandleTypeDef huart1;

void MX_USART0_UART_Init(void)
{
    usart_deinit(USART0);
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_USART0);

    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

    usart_baudrate_set(USART0,115200);
    usart_parity_config(USART0,USART_PM_NONE);
    usart_word_length_set(USART0,USART_WL_8BIT);
    usart_stop_bit_set(USART0,USART_STB_1BIT);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
    usart_data_first_config(USART0,USART_MSBF_LSB);
    usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);
    usart_receive_config(USART0,USART_RECEIVE_ENABLE);

    usart_interrupt_enable(USART0,USART_INT_RBNE);
    usart_interrupt_enable(USART0, USART_INT_TBE);

    usart_enable(USART0);
    nvic_irq_enable(USART0_IRQn,0,0);
}

void Usart0_Sendbyte(uint8_t data)
{
    while(usart_flag_get(USART0,USART_FLAG_TBE) == RESET);
    usart_data_transmit(USART0,data);
}

int fputc(int c, FILE *f)
{
  Usart0_Sendbyte(c);
  return c;
}
